package com.tvunetworks.center.user.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.netflix.discovery.EurekaClient;
import com.tvunetworks.center.common.util.JsonUtil;
import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.center.common.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PreDestroy;
import java.io.IOException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @program: tvucc-user
 * @description: BaseController
 * @author lebronchen
 * @create: 2019-03-17 17:04
 **/
@RestController
@RequestMapping("/base")
@Slf4j
public class BaseController {

    @Value("${redirectUrl}")
    private String redirectUrl;
    @Value("${portForhttpOrHttps}")
    private String portForhttpOrHttps;
    @Value("${s3.regional.info}")
    private String awsRegionalInfo;
    @Value("${baidu.map.account}")
    private String baiduMapAccount;
    @Value("${google.map.account}")
    private String googleMapAccount;
    @Value("${enableCustomizedCC}")
    private boolean enableCustomizedCC;
    @Value("${enableNewMultiView}")
    private boolean enableNewMultiView;
    @Value("${buildversion}")
    private String buildversion;
    @Value("${commonBuildversion}")
    private String commonBuildversion;
    @Value("${dashboardUrl}")
    private String dashboardUrl;
    @Value("${newuserservice.logout.url}")
    private String newuserserviceLogoutUrl;
    @Value("${newuserservice.login.url}")
    private String newuserserviceLoginUrl;
    @Value("${servicenameinauthorizationservice}")
    private String authServerName;
    @Value("${newuserservice.redirect.url.cc}")
    private String newUserServiceUrl;
    @Value("${isCompressThumbnail}")
    private String isCompressThumbnail;
    @Value("${isForceHttps}")
    private String isForceHttps;
    @Value("${frontGeoServiceUrl}")
    private String frontGeoServiceUrl;
    @Value("${playout.url}")
    private String playoutUrl;
    @Value("${playout.server.name}")
    private String playoutServerName;

    /**
     *
     * @return version
     */
    @GetMapping("/version")
    public String version (){
        return buildversion + "|" + commonBuildversion;
    }
    @Value("${newTvuccUrl.redirect.url}")
    private String newTvuccUrl;
    @Value("${version.code}")
    private String versionCode;
    @Value("${version.api.url}")
    private String versionApiUrl;
    @Value("${version.url}")
    private String versionUrl;
    @Value("${cloud.drive.Url.front}")
//    @Value("http://cloud-drive-test.tvunetworks.com")
    private String cloudDriveGateWayUrl;

    @Value("${cloud.drive.Url.backend}")
//    @Value("http://cloud-drive-test.tvunetworks.com")
    private String cloudDriveUrl;
    @Value("${user_auth_url}")
    private String userAuthUrl;
    @Value("${service_list_url}")
    private String serviceListUrl;
    @Value("${platform_list}")
    private String platformList;
    @Value("${mediaInitConfig}")
    private String mediaInitConfig;
    @Value("${instance.cloudR.multiR.instanceType}")
    private String instanceType;


    @Autowired
    private  EurekaClient eurekaClient;

    /**
     * 销毁Eureka注册
     */
    @PreDestroy
    public void stop() {
        if (eurekaClient != null) {
            log.error("Shutting down server and notify to Eureka  by  tvucc-user.");
            eurekaClient.shutdown();
        }
    }

    /**
     *
     * @return 配置
     * @throws IOException 编译异常
     */
    @GetMapping("/initConfig")
    public Result<Object> initConfig() throws IOException {

        Map map = JsonUtil.toObject(redirectUrl, Map.class);
        JSONArray jsonArray = JSONArray.parseArray(portForhttpOrHttps);
        Map<String, String> result = JsonUtil.toObject(instanceType,Map.class);
        if (result!=null){
            Set<String> list = new HashSet<>();
            for (String value : result.values()) {
                list.add(value);
            }
            if (!CollectionUtils.isEmpty(list)) {
                String join = StringUtils.join(list, ",");
                map.put("instanceType", join);
            }
        }

        map.put("portForhttpOrHttps",jsonArray);
        map.put("gtag",mediaInitConfig);
        map.put("awsRegionalInfo", JSON.parse(awsRegionalInfo));
        map.put("baiduMapAccount", baiduMapAccount);
        map.put("googleMapAccount", googleMapAccount);
        map.put("enableCustomizedCC", enableCustomizedCC);
        map.put("enableNewMultiView", enableNewMultiView);
        map.put("dashboardUrl", dashboardUrl);
        map.put("newuserserviceLogoutUrl", newuserserviceLogoutUrl);
        map.put("newuserserviceLoginUrl", newuserserviceLoginUrl);
        map.put("authServerName", authServerName);
        map.put("newUserServiceUrl", newUserServiceUrl);
        map.put("isCompressThumbnail", isCompressThumbnail);
        map.put("isForceHttps", isForceHttps);
        map.put("frontGeoServiceUrl", frontGeoServiceUrl);
        map.put("playoutUrl", playoutUrl);
        map.put("playoutServerName", playoutServerName);
        map.put("newTvuccUrl", newTvuccUrl);
        map.put("versionCode", versionCode);
        map.put("versionApiUrl", versionApiUrl);
        map.put("versionUrl", versionUrl);
        map.put("cloudDriveGateWayUrl", cloudDriveGateWayUrl);
        map.put("cloudDriveUrl", cloudDriveUrl);
        map.put("userAuthUrl", userAuthUrl);
        map.put("serviceListUrl", serviceListUrl);
        map.put("platformList",platformList);
        return ResultUtil.success(map);
    }

    /**
     *
     * 提供一个500 的接口给前端测试
     *
     * @return Result
     */
    @GetMapping("/500")
    public Result<Object> serverError()  {
        throw new NullPointerException();
    }

}
